Fork me on GitHub

trivial

题目

An unlocked terminal is displaying the following:

Encryption complete, ENC(???,T0pS3cre7key) = Bot kmws mikferuigmzf rmfrxrwqe abs perudsf! Nvm kda ut ab8bv_w4ue0_ab8v_DDU

You poke around and find this interesting file.

题目链接

http://www.shiyanbar.com/ctf/1980

题解

题目给了一个加密的python代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import sys
alphaL = "abcdefghijklnmopqrstuvqxyz"
alphaU = "ABCDEFGHIJKLMNOPQRSTUVQXYZ"
num = "0123456789"
keychars = num+alphaL+alphaU
if len(sys.argv) != 3:
print "Usage: %s SECRET_KEY PLAINTEXT"%(sys.argv[0])
sys.exit()
key = sys.argv[1]
if not key.isalnum():
print "Your key is invalid, it may only be alphanumeric characters"
sys.exit()
plaintext = sys.argv[2]
ciphertext = ""
for i in range(len(plaintext)):
rotate_amount = keychars.index(key[i%len(key)])
if plaintext[i] in alphaL:
enc_char = ord('a') + (ord(plaintext[i])-ord('a')+rotate_amount)%26
elif plaintext[i] in alphaU:
enc_char = ord('A') + (ord(plaintext[i])-ord('A')+rotate_amount)%26
elif plaintext[i] in num:
enc_char = ord('0') + (ord(plaintext[i])-ord('0')+rotate_amount)%10
else:
enc_char = ord(plaintext[i])
ciphertext = ciphertext + chr(enc_char)
print "Encryption complete, ENC(%s,%s) = %s"%(plaintext,key,ciphertext)

其实就是个移位加密,看懂了加密之后稍微修改一下就可以解密了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import sys
alphaL = "abcdefghijklnmopqrstuvqxyz"
alphaU = "ABCDEFGHIJKLMNOPQRSTUVQXYZ"
num = "0123456789"
keychars = num+alphaL+alphaU
key = "T0pS3cre7key"
plaintext = "Bot kmws mikferuigmzf rmfrxrwqe abs perudsf! Nvm kda ut ab8bv_w4ue0_ab8v_DDU"
ciphertext = ""
for i in range(len(plaintext)):
rotate_amount = keychars.index(key[i%len(key)])
if plaintext[i] in alphaL:
enc_char = ord('a') + (ord(plaintext[i])-ord('a')-rotate_amount)%26
elif plaintext[i] in alphaU:
enc_char = ord('A') + (ord(plaintext[i])-ord('A')-rotate_amount)%26
elif plaintext[i] in num:
enc_char = ord('0') + (ord(plaintext[i])-ord('0')-rotate_amount)%10
else:
enc_char = ord(plaintext[i])
ciphertext = ciphertext + chr(enc_char)
print("Encryption complete, ENC(%s,%s) = %s"%(plaintext,key,ciphertext))

得到:

Encryption complete, ENC(Bot kmws mikferuigmzf rmfrxrwqe abs perudsf! Nvm kda ut ab8bv_w4ue0_ab8v_DDU,T0pS3cre7key) = You hawe successfully decrypwed the message! The key is th4ts_w0rs3_th4n_DES

-------------本文结束感谢您的阅读-------------